package com.seckill.controller;

import com.seckill.access.AccessLimit;
import com.seckill.domain.MiaoshaUser;
import com.seckill.result.CodeMsg;
import com.seckill.result.Result;
import com.seckill.service.MiaoshaService;
import com.seckill.service.VerifyCodeService;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Objects;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author yangjunwei
 * @date 2021/11/2 9:30 上午
 */
@RestController
@RequestMapping("/miaosha")
@Slf4j
public class MiaoshaController {

    @Autowired
    MiaoshaService miaoshaService;

    @Autowired
    VerifyCodeService verifyCodeService;

    /**
     * 秒杀接口
     *
     * @param miaoshaUser  自定义参数构造器-封装用户信息
     * @param model-视图
     * @param goodsId-商品Id
     * @return 订单详情页面
     */
    @PostMapping("/{path}/do_miaosha")
    public Result<Integer> doMiaosha(MiaoshaUser miaoshaUser, Model model, @PathVariable("path") String path, @RequestParam("goodsId") long goodsId) {
        model.addAttribute("user", miaoshaUser);
        if (Objects.isNull(miaoshaUser)) {
            return Result.error(CodeMsg.SESSION_ERROR);
        }
        return miaoshaService.domiaosha(miaoshaUser, path, goodsId);
    }

    /**
     * 获取秒杀结果 成功：订单Id 失败：-1 进行中：0
     */
    @GetMapping("/result")
    public Result<Long> miaoshaResult(Model model, MiaoshaUser miaoshaUser, @RequestParam("goodsId") long goodsId) {
        model.addAttribute("user", miaoshaUser);
        if (miaoshaUser == null) {
            return Result.error(CodeMsg.SESSION_ERROR);
        }
        long miaoshaResult = miaoshaService.getMiaoshaResult(miaoshaUser.getId(), goodsId);
        return Result.success(miaoshaResult);
    }

    @GetMapping("/verifyCode")
    public Result<String> verifyCode(HttpServletResponse httpServletResponse, MiaoshaUser miaoshaUser, @RequestParam("goodsId") long goodsId) {
        if (miaoshaUser == null) {
            return Result.error(CodeMsg.SESSION_ERROR);
        }
        try {
            BufferedImage verifyCode = verifyCodeService.createVerifyCode(miaoshaUser, goodsId);
            OutputStream outputStream = httpServletResponse.getOutputStream();
            ImageIO.write(verifyCode, "JPEG", outputStream);
            outputStream.flush();
            outputStream.close();
            return null;
        } catch (IOException ioException) {
            ioException.printStackTrace();
            return Result.error(CodeMsg.MOBILE_ERROR);
        }
    }

    /**
     * 生成秒杀路径
     *
     * @param miaoshaUser 用户
     * @param verifyCode  验证码
     * @param goodsId     商品Id
     * @return 秒杀路径
     */
    @AccessLimit(seconds = 5, maxCount = 5)
    @GetMapping("/path")
    public Result<String> miaoshaPath(MiaoshaUser miaoshaUser, @RequestParam(value = "verifyCode", defaultValue = "0") int verifyCode,
        @RequestParam("goodsId") long goodsId) {
        if (Objects.isNull(miaoshaUser)) {
            return Result.error(CodeMsg.SESSION_ERROR);
        }
        //校验验证码
        boolean checkVerifyCode = verifyCodeService.checkVerifyCode(miaoshaUser, goodsId, verifyCode);
        if (!checkVerifyCode) {
            return Result.error(CodeMsg.VERIFY_ERROR);
        }
        String miaoshaPath = miaoshaService.getMiaoshaPath(miaoshaUser.getId(), goodsId);
        return Result.success(miaoshaPath);
    }


}
